// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 添加淡入动画效果
    const fadeElements = document.querySelectorAll('.card, .carousel-item');
    fadeElements.forEach(element => {
        element.classList.add('fade-in');
    });
    
    // 轮播图自动播放
    const carousel = document.querySelector('#mainCarousel');
    if (carousel) {
        new bootstrap.Carousel(carousel, {
            interval: 5000,
            wrap: true
        });
    }
    
    // 表单验证
    const forms = document.querySelectorAll('form');
    forms.forEach(form => {
        form.addEventListener('submit', function(event) {
            if (!form.checkValidity()) {
                event.preventDefault();
                event.stopPropagation();
            }
            form.classList.add('was-validated');
        });
    });
    
    // 图片预览
    const imageInput = document.querySelector('input[type="file"]');
    if (imageInput) {
        imageInput.addEventListener('change', function(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.createElement('img');
                    preview.src = e.target.result;
                    preview.style.maxWidth = '100%';
                    preview.style.marginTop = '10px';
                    
                    const previewContainer = document.createElement('div');
                    previewContainer.appendChild(preview);
                    
                    const existingPreview = imageInput.parentElement.querySelector('img');
                    if (existingPreview) {
                        existingPreview.remove();
                    }
                    
                    imageInput.parentElement.appendChild(previewContainer);
                };
                reader.readAsDataURL(file);
            }
        });
    }
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
            }
        });
    });
}); 